from groq import Groq
from dotenv import load_dotenv
import os

# 加载环境变量
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
    raise RuntimeError("找不到 GROQ_API_KEY")

# 创建 Groq 客户端
client = Groq(api_key=api_key)

print("Groq 聊天机器人已启动！（输入 exit 退出）")
print("-" * 40)

history = []

while True:
    user = input("你：")
    if user.lower().strip() == "exit":
        print("结束聊天，再见！")
        break

    # 将用户消息加入历史
    history.append({"role": "user", "content": user})

    # 调用 Llama3.1 模型
    response = client.chat.completions.create(
        model="llama-3.1-8b-instant",
        messages=history
    )

    reply = response.choices[0].message.content
    print("Groq：" + reply + "\n")

    # 将模型回复加入历史
    history.append({"role": "assistant", "content": reply})
